//
//  WebQRController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-20.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "WebQRController.h"
#import "NetHander.h"
@interface WebQRController ()
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation WebQRController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查询结果";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
//    // 创建通知中心
//    NSNotificationCenter *noti = [[NSNotificationCenter alloc]init];
//    [noti addObserver:self selector:@selector(notiTarget:) name:@"QR" object:nil];
    // 判断是条形码，还是二维码
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self handleData];
    });
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"cancle.png"] style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
}
- (void)handleData
{
    if ([self.result hasPrefix:@"http"]) {
        // 如果是二维码，就加载网页
        [self loadWeb];
    }else {
        // 如果是条形码，就解析数据
        [self loadData1];
    }
}
- (void)targetCancle:(UIButton *)cancle
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 加载网页
- (void)loadWeb
{
    // 在网页上加载网址
    NSString *path = [self.result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    request.HTTPMethod = @"GET";
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 加载数据
- (void)loadData1
{
    // 添加两个显示信息的label
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, WIDTH - 20, 30)];
    UILabel *price = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, WIDTH - 20, 30)];
    [self.view addSubview:title];
    [self.view addSubview:price];
    
    UILabel *title1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 120, WIDTH - 20, 30)];
    UILabel *price1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 170, WIDTH - 20, 30)];
    [self.view addSubview:title1];
    [self.view addSubview:price1];
    
    UILabel *title2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 220, WIDTH - 20, 30)];
    UILabel *price2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 270, WIDTH - 20, 30)];
    [self.view addSubview:title2];
    [self.view addSubview:price2];
    NSString *path = [NSString stringWithFormat:@"http://wap.wochacha.com/shopping/latestprices?barcode=%@&udid=FFFFFFFFA7784BF11EAC425DA6E372BD8E989F74&longitude=121.540338&latitude=38.883490", self.result];
    
    [NetHander getDataWithUrl:path completion:^(NSData *data) {
        NSArray *dataArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        title.text = [[dataArr objectAtIndex:0] objectForKey:@"Title"];
        price.text = [[dataArr objectAtIndex:0] objectForKey:@"Price"];
        title1.text = [[dataArr objectAtIndex:1] objectForKey:@"Title"];
        price1.text = [[dataArr objectAtIndex:1] objectForKey:@"Price"];
        title2.text = [[dataArr objectAtIndex:2] objectForKey:@"Title"];
        price2.text = [[dataArr objectAtIndex:2] objectForKey:@"Price"];
        if (title.text.length == 0 && title1.text.length == 0 && title2.text.length == 0) {
            title.text = @"抱歉，没有查到该条形码的信息";
            price.text = nil;
            price1.text = nil;
            price2.text = nil;
        }
        
//        NSLog(@"==%@", [[dataArr objectAtIndex:0] objectForKey:@"Title"]);
    }];
}
#pragma mark 通知中心方法
//- (void)notiTarget:(NSNotification *)noti
//{
//    self.result = [noti.userInfo objectForKey:@"name"];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
